﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Turningtoolswithcarbidetips
    {
        public static void _Turningtoolswithcarbidetips(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour");//轮廓层
            Layer dimensioning = new Layer("dimensioning");//尺寸层
            Layer refin = new Layer("refin");//基准线/内轮廓/虚线
            Layer center = new Layer("center");//中心线
            Layer textlayer = new Layer("textlayer");//文本层
            Layer hatching = new Layer("hatching");//填充层
            Layer additionalline = new Layer("additionalline");//附加线
            Layer cutting = new Layer("cutting");//切削层
            dxf.Layers["0"].Lineweight = Lineweight.W25;

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);



            double scale = Convert.ToDouble(data["scale"]); //放大系数
            double lf = Convert.ToDouble(data["lf"]) * scale;
            double h = Convert.ToDouble(data["h"]) * scale;
            double b = Convert.ToDouble(data["b"]) * scale;
            double h1 = Convert.ToDouble(data["hf"]) * scale;
            double l = Convert.ToDouble(data["l"]) * scale;
            double n = Convert.ToDouble(data["wf2"]) * scale;
            var hand = "0";
            try
            {
                hand = data["hand"].ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine("[hand] not found");
            }
            
            //坐标
            //1
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X - lf / 3, v1.Y);
            Vector2 v4 = new Vector2(v1.X, v1.Y - h);
            Vector2 v3 = new Vector2(v4.X - lf / 3 + 0.5, v4.Y);
            Vector2 v5 = new Vector2(v4.X + 2, v4.Y);
            Vector2 v6 = new Vector2(v5.X + lf / 3, v5.Y);
            Vector2 v7 = new Vector2(v6.X + 1.5, v6.Y + 1);
            Vector2 v10 = new Vector2(v1.X + 2, v1.Y);
            Vector2 v9 = new Vector2(v10.X + lf / 3, v10.Y);
            Vector2 v8 = new Vector2(v9.X + 1.5, v9.Y - 1);
            Vector2 v11 = new Vector2(v2.X + 1.5 / h * 0.5, v2.Y - 1.5);
            Vector2 v12 = new Vector2(v11.X - h / 3, v11.Y);
            Vector2 v13 = new Vector2(v12.X, v2.Y);
            Vector2 v14 = new Vector2(v13.X - l * Math.Cos(30 * pi / 180), v13.Y);
            Vector2 v15 = fun.Polar(v14, -80 * pi / 180, l * 3 / 8);
            Vector2 v16 = new Vector2(v12.X, v15.Y);
            Vector2 v17 = new Vector2(v15.X + 0.5, v15.Y);
            Vector2 v18 = new Vector2(v17.X + 1, v3.Y);
            Vector2 v19 = new Vector2(v1.X, v1.Y + 2);
            Vector2 v20 = new Vector2(v4.X, v4.Y - 2);
            Vector2 v21 = new Vector2(v10.X, v10.Y + 2);
            Vector2 v22 = new Vector2(v5.X, v5.Y - 2);

            Vector2 v23 = new Vector2(v4.X, v4.Y - h * 2);
            Vector2 v24 = new Vector2(v23.X, v23.Y - b);
            Vector2 v25 = new Vector2(v23.X + 2, v23.Y);
            Vector2 v26 = new Vector2(v24.X + 2, v24.Y);
            Vector2 v27 = new Vector2(v2.X, v23.Y);
            Vector2 v28 = new Vector2(v27.X, v24.Y);
            Vector2 v29 = new Vector2(v6.X, v26.Y);
            Vector2 v30 = new Vector2(v29.X + 1.5, v29.Y + 1);
            Vector2 v32 = new Vector2(v29.X, v25.Y);
            Vector2 v31 = new Vector2(v32.X + 1.5, v32.Y - 1);
            Vector2 v36 = new Vector2(v14.X, v28.Y + n);
            Vector2 v37 = fun.Polar(v36, -30 * pi / 180, l);
            Vector2 v35 = fun.Polar(v36, 60 * pi / 180, l / 2);
            Vector2 v39 = fun.Polar(v35, -30 * pi / 180, l * 3 / 8);
            Vector2 v38 = fun.Polar(v37, 60 * pi / 180, 0.5);
            Vector2 v34 = fun.Polar(v35, -30 * pi / 180, l * 3 / 16);
            Vector2 v40 = new Vector2(v23.X, v23.Y + 2);
            Vector2 v41 = new Vector2(v24.X, v24.Y - 2);
            Vector2 v42 = new Vector2(v25.X, v25.Y + 2);
            Vector2 v43 = new Vector2(v26.X, v26.Y - 2);
            Vector2 v33 = new Vector2(v34.X + 2, v27.Y);
            Vector2 v44 = new Vector2(v23.X, v23.Y - b / 2);
            Vector2 v45 = new Vector2(v32.X, v32.Y - b / 2);
            Vector2 v36_1=fun.mirrorpoint(v36,v44,v45);
            Vector2 v28_1 = fun.mirrorpoint(v28, v44, v45);
            Vector2 v37_1 = fun.mirrorpoint(v37, v44, v45);
            Vector2 v38_1 = fun.mirrorpoint(v38, v44, v45);
            Vector2 v39_1 = fun.mirrorpoint(v39, v44, v45);


            if (hand.Equals("0"))
             {
                //作图
                //1
                Line line1 = new Line(v19, v20);
                line1.Layer = center;
                line1.Linetype = Linetype.DashDot;
                line1.LinetypeScale = 3;
                dxf.AddEntity(line1);

                Line line2 = new Line(v21, v22);
                line2.Layer = center;
                line2.Linetype = Linetype.DashDot;
                line2.LinetypeScale = 3;
                dxf.AddEntity(line2);

                LwPolyline line3 = new LwPolyline();
                line3.Vertexes.Add(new LwPolylineVertex(v1));
                line3.Vertexes.Add(new LwPolylineVertex(v2));
                line3.Vertexes.Add(new LwPolylineVertex(v3));
                line3.Vertexes.Add(new LwPolylineVertex(v4));
                fun.PolyFill(dxf, line3, 0.3, contour);

                LwPolyline line4 = new LwPolyline();
                line4.Vertexes.Add(new LwPolylineVertex(v5));
                line4.Vertexes.Add(new LwPolylineVertex(v6));
                line4.Vertexes.Add(new LwPolylineVertex(v9));
                line4.Vertexes.Add(new LwPolylineVertex(v10));
                fun.PolyFill(dxf, line4, 0.3, contour);
                 
                LwPolyline line6 = new LwPolyline();
                line6.Vertexes.Add(new LwPolylineVertex(v11));
                line6.Vertexes.Add(new LwPolylineVertex(v12));
                line6.Vertexes.Add(new LwPolylineVertex(v13));
                line6.Vertexes.Add(new LwPolylineVertex(v14));
                line6.Vertexes.Add(new LwPolylineVertex(v15));
                line6.Vertexes.Add(new LwPolylineVertex(v16));
                line6.Vertexes.Add(new LwPolylineVertex(v12));
                fun.PolyFill(dxf, line6, 0.3, contour);

                LwPolyline line7 = new LwPolyline();
                line7.Vertexes.Add(new LwPolylineVertex(v17));
                line7.Vertexes.Add(new LwPolylineVertex(v18));
                line7.Vertexes.Add(new LwPolylineVertex(v3));
                fun.PolyFill(dxf, line7, 0.3, contour);

                Line line8 = new Line(v40, v41);
                line8.Layer = center;
                line8.Linetype = Linetype.DashDot;
                line8.LinetypeScale = 3;
                dxf.AddEntity(line8);

                Line line9 = new Line(v42, v43);
                line9.Layer = center;
                line9.Linetype = Linetype.DashDot;
                line9.LinetypeScale = 3;
                dxf.AddEntity(line9);

                LwPolyline line10 = new LwPolyline();
                line10.Vertexes.Add(new LwPolylineVertex(v26));
                line10.Vertexes.Add(new LwPolylineVertex(v29));
                line10.Vertexes.Add(new LwPolylineVertex(v32));
                line10.Vertexes.Add(new LwPolylineVertex(v25));
                fun.PolyFill(dxf, line10, 0.3, contour);

                LwPolyline line11 = new LwPolyline();
                line11.Vertexes.Add(new LwPolylineVertex(v29));
                line11.Vertexes.Add(new LwPolylineVertex(v30));
                line11.Vertexes.Add(new LwPolylineVertex(v31));
                line11.Vertexes.Add(new LwPolylineVertex(v32));
                fun.PolyFill(dxf, line11, 0.3, contour);

                LwPolyline line12 = new LwPolyline();
                line12.Vertexes.Add(new LwPolylineVertex(v23));
                line12.Vertexes.Add(new LwPolylineVertex(v27));
                line12.Vertexes.Add(new LwPolylineVertex(v28));
                line12.Vertexes.Add(new LwPolylineVertex(v24));
                fun.PolyFill(dxf, line12, 0.3, contour);

                LwPolyline line13 = new LwPolyline();
                line13.Vertexes.Add(new LwPolylineVertex(v27));
                line13.Vertexes.Add(new LwPolylineVertex(v33));
                line13.Vertexes.Add(new LwPolylineVertex(v34));
                fun.PolyFill(dxf, line13, 0.3, contour);

                LwPolyline line14 = new LwPolyline();
                line14.Vertexes.Add(new LwPolylineVertex(v35));
                line14.Vertexes.Add(new LwPolylineVertex(v36));
                line14.Vertexes.Add(new LwPolylineVertex(v37));
                line14.Vertexes.Add(new LwPolylineVertex(v38));
                line14.Vertexes.Add(new LwPolylineVertex(v39));
                line14.Vertexes.Add(new LwPolylineVertex(v35));
                line14.Vertexes[3].Bulge = Math.Tan(80 * pi / 180 / 4);
                fun.PolyFill(dxf, line14, 0.3, contour);

                LwPolyline line15 = new LwPolyline();
                line15.Vertexes.Add(new LwPolylineVertex(v38));
                line15.Vertexes.Add(new LwPolylineVertex(v28));
                fun.PolyFill(dxf, line15, 0.3, contour);



                //标注样式
                //箭头样式
                DimensionStyle dimensionStyle = fun.CreateDimStyle();
                dimensionStyle.ArrowSize = 3.5;
                dimensionStyle.ExtLineExtend = 0.6;//标注延长线长度/2
                dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
                dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
                dimensionStyle.TextHeight = 3.5;
                double offset = 10;


                //文本样式

                LinearDimension dim1 = new LinearDimension(new Line(v6, v9), h, -90, dimensionStyle);
                dim1.UserText = "h=" + Math.Round(h / scale, 1);
                dxf.AddEntity(dim1);

                LinearDimension dim2 = new LinearDimension(new Line(v14, v7), h * 2, 180, dimensionStyle);
                dim2.UserText = "L=" + Math.Round(lf / scale, 1);
                dxf.AddEntity(dim2);

                LinearDimension dim3 = new LinearDimension(new Line(v14, v18), h, 90, dimensionStyle);
                dim3.UserText = "h1=" + Math.Round(h1 / scale, 1);
                dxf.AddEntity(dim3);

                LinearDimension dim4 = new LinearDimension(new Line(v32, v29), h, -90, dimensionStyle);
                dim4.UserText = "b=" + Math.Round(b / scale, 1);
                dxf.AddEntity(dim4);

                LinearDimension dim5 = new LinearDimension(new Line(v36, v28), h * 2, -90, dimensionStyle);
                dim5.UserText = "n=" + Math.Round(n / scale, 1);
                dxf.AddEntity(dim5);

                LinearDimension dim6 = new LinearDimension(new Line(v36, v37), h * 3 / 2, 120, dimensionStyle);
                dim6.UserText = "l=" + Math.Round(l / scale, 1);
                dxf.AddEntity(dim6);

                fun.jdbz1(dxf, new Line(v36, fun.Polar(v36, -90 * pi / 180, 0.1)), new Line(v36, fun.Polar(v36, -30 * pi / 180, 0.1)), 10, "70°", 1);



            }
            else
            {
                Line line1 = new Line(v19, v20);
                line1.Layer = center;
                line1.Linetype = Linetype.DashDot;
                line1.LinetypeScale = 3;
                dxf.AddEntity(line1);

                Line line2 = new Line(v21, v22);
                line2.Layer = center;
                line2.Linetype = Linetype.DashDot;
                line2.LinetypeScale = 3;
                dxf.AddEntity(line2);

                LwPolyline line3 = new LwPolyline();
                line3.Vertexes.Add(new LwPolylineVertex(v1));
                line3.Vertexes.Add(new LwPolylineVertex(v2));
                line3.Vertexes.Add(new LwPolylineVertex(v3));
                line3.Vertexes.Add(new LwPolylineVertex(v4));
                fun.PolyFill(dxf, line3, 0.3, contour);

                LwPolyline line4 = new LwPolyline();
                line4.Vertexes.Add(new LwPolylineVertex(v5));
                line4.Vertexes.Add(new LwPolylineVertex(v6));
                line4.Vertexes.Add(new LwPolylineVertex(v9));
                line4.Vertexes.Add(new LwPolylineVertex(v10));
                fun.PolyFill(dxf, line4, 0.3, contour);

                LwPolyline line5 = new LwPolyline();
                line5.Vertexes.Add(new LwPolylineVertex(v6));
                line5.Vertexes.Add(new LwPolylineVertex(v7));
                line5.Vertexes.Add(new LwPolylineVertex(v8));
                line5.Vertexes.Add(new LwPolylineVertex(v9));
                fun.PolyFill(dxf, line5, 0.3, contour);

                LwPolyline line6 = new LwPolyline();
                line6.Vertexes.Add(new LwPolylineVertex(v11));
                line6.Vertexes.Add(new LwPolylineVertex(v12));
                line6.Vertexes.Add(new LwPolylineVertex(v13));
                line6.Vertexes.Add(new LwPolylineVertex(v14));
                line6.Vertexes.Add(new LwPolylineVertex(v15));
                line6.Vertexes.Add(new LwPolylineVertex(v16));
                line6.Vertexes.Add(new LwPolylineVertex(v12));
                fun.PolyFill(dxf, line6, 0.3, contour);

                LwPolyline line7 = new LwPolyline();
                line7.Vertexes.Add(new LwPolylineVertex(v17));
                line7.Vertexes.Add(new LwPolylineVertex(v18));
                line7.Vertexes.Add(new LwPolylineVertex(v3));
                fun.PolyFill(dxf, line7, 0.3, contour);


                Line line8 = new Line(v40, v41);
                line8.Layer = center;
                line8.Linetype = Linetype.DashDot;
                line8.LinetypeScale = 3;
                dxf.AddEntity(line8);

                Line line9 = new Line(v42, v43);
                line9.Layer = center;
                line9.Linetype = Linetype.DashDot;
                line9.LinetypeScale = 3;
                dxf.AddEntity(line9);

                LwPolyline line10 = new LwPolyline();
                line10.Vertexes.Add(new LwPolylineVertex(v26));
                line10.Vertexes.Add(new LwPolylineVertex(v29));
                line10.Vertexes.Add(new LwPolylineVertex(v32));
                line10.Vertexes.Add(new LwPolylineVertex(v25));
                fun.PolyFill(dxf, line10, 0.3, contour);

                LwPolyline line11 = new LwPolyline();
                line11.Vertexes.Add(new LwPolylineVertex(v29));
                line11.Vertexes.Add(new LwPolylineVertex(v30));
                line11.Vertexes.Add(new LwPolylineVertex(v31));
                line11.Vertexes.Add(new LwPolylineVertex(v32));
                fun.PolyFill(dxf, line11, 0.3, contour);

                LwPolyline line12 = new LwPolyline();
                line12.Vertexes.Add(new LwPolylineVertex(v23));
                line12.Vertexes.Add(new LwPolylineVertex(v27));
                line12.Vertexes.Add(new LwPolylineVertex(v28));
                line12.Vertexes.Add(new LwPolylineVertex(v24));

                LwPolyline line13 = new LwPolyline();
                line13.Vertexes.Add(new LwPolylineVertex(v27));
                line13.Vertexes.Add(new LwPolylineVertex(v33));
                line13.Vertexes.Add(new LwPolylineVertex(v34));

                LwPolyline line14 = new LwPolyline();
                line14.Vertexes.Add(new LwPolylineVertex(v39));
                line14.Vertexes.Add(new LwPolylineVertex(v35));
                line14.Vertexes.Add(new LwPolylineVertex(v36));
                line14.Vertexes.Add(new LwPolylineVertex(v37));
                line14.Vertexes.Add(new LwPolylineVertex(v38));

                LwPolyline line15 = new LwPolyline();
                line15.Vertexes.Add(new LwPolylineVertex(v38));
                line15.Vertexes.Add(new LwPolylineVertex(v28));

                LwPolyline line16 = new LwPolyline();
                line16.Vertexes.Add(new LwPolylineVertex(v44));
                line16.Vertexes.Add(new LwPolylineVertex(v45));

                LwPolyline line17 = new LwPolyline();
                line17.Vertexes.Add(new LwPolylineVertex(v38_1));
                line17.Vertexes.Add(new LwPolylineVertex(v39_1));
                line17.Vertexes[0].Bulge = Math.Tan(-80 * pi / 180 / 4);
                fun.PolyFill(dxf, line17, 0.3, contour);

                var line12_1 = Pus.MirrorPolyline(line12, v44, v45);
                dxf.AddEntity(line12_1);
                fun.PolyFill(dxf, line12_1, 0.3, contour);

                var line13_1 = Pus.MirrorPolyline(line13, v44, v45);
                dxf.AddEntity(line13_1);
                fun.PolyFill(dxf, line13_1, 0.3, contour);

                var line14_1 = Pus.MirrorPolyline(line14, v44, v45);
                dxf.AddEntity(line14_1);
                fun.PolyFill(dxf, line14_1, 0.3, contour);

                var line15_1 = Pus.MirrorPolyline(line15, v44, v45);
                dxf.AddEntity(line15_1);
                fun.PolyFill(dxf, line15_1, 0.3, contour);




                //标注样式
                //箭头样式
                DimensionStyle dimensionStyle = fun.CreateDimStyle();
                dimensionStyle.ArrowSize = 3.5;
                dimensionStyle.ExtLineExtend = 0.6;//标注延长线长度/2
                dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
                dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
                dimensionStyle.TextHeight = 3.5;
                double offset = 10;



                //文本样式

                LinearDimension dim1 = new LinearDimension(new Line(v6, v9), h, -90, dimensionStyle);
                dim1.UserText = "h=" + Math.Round(h / scale, 1);
                dxf.AddEntity(dim1);

                LinearDimension dim2 = new LinearDimension(new Line(v14, v7), h * 3 / 2, 180, dimensionStyle);
                dim2.UserText = "L=" + Math.Round(lf / scale, 1);
                dxf.AddEntity(dim2);

                LinearDimension dim3 = new LinearDimension(new Line(v14, v18), h, 90, dimensionStyle);
                dim3.UserText = "h1=" + Math.Round(h1 / scale, 1);
                dxf.AddEntity(dim3);

                LinearDimension dim4 = new LinearDimension(new Line(v32, v29), h, -90, dimensionStyle);
                dim4.UserText = "b=" + Math.Round(b / scale, 1);
                dxf.AddEntity(dim4);

                LinearDimension dim5 = new LinearDimension(new Line(v36_1, v28_1), h * 2, -90, dimensionStyle);
                dim5.UserText = "n=" + Math.Round(n / scale, 1);
                dxf.AddEntity(dim5);

                LinearDimension dim6 = new LinearDimension(new Line(v36_1, v37_1), h , 60, dimensionStyle);
                dim6.UserText = "l=" + Math.Round(l / scale, 1);
                dxf.AddEntity(dim6);

                fun.jdbz1(dxf, new Line(v36_1, fun.Polar(v36_1, 90 * pi / 180, 0.1)), new Line(v36_1, fun.Polar(v36_1, 30 * pi / 180, 0.1)), 10, "70°", 1);



            }

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            fun.SetLayer(dxf);
            fun.SetByLayer(dxf);

            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}
